"""
Case Type   : 内核告警
Case Name   : 设置alarm_report_max_count参数，测试配置是否生效
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、查询alarm_report_max_count参数是否设置为默认值1
    2、修改alarm_report_max_count参数值为0，观测是否报错
    3、修改alarm_report_max_count为10，观测是否正常
    4、清理环境
Expect      :
    1、显示默认值1;
    2、修改参数失败，报错提示合理;
    3、修改参数成功，参数生效;
    4、清理环境成功
History     :
"""

import os
import unittest
import time

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.node = Node('PrimaryDbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_alarm_report(self):
        text = '--step1:查询alarm_report_max_count参数是否设置为默认值;expect:默认为1--'
        logger.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"cm_ctl list --param --agent |grep alarm_report_max_count"
        logger.info(cmd)

        res = self.node.sh(cmd).result()
        logger.info(res)
        self.assertIn('alarm_report_max_count = 1', res.splitlines()[-1].strip(),
                      '执行失败' + text)

        text = '--step2:修改alarm_report_max_count参数值为0;expect:失败--'
        logger.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"cm_ctl set --param --agent -k 'alarm_report_max_count=0'"
        res = self.node.sh(cmd).result()
        logger.info(res)
        self.assertIn('is incorrect, requires a integer value', res, '执行失败:' + text)

        text = '--step3:修改alarm_report_max_count参数值为3;expect:成功--'
        logger.info(text)
        res = self.comsh.exec_cm_ctl_set('agent', 'alarm_report_max_count=3')
        self.assertTrue(res, '执行失败:' + text)
        dn_name = macro.DN_NODE_NAME.split('/')[0]
        cmd = f'cd {macro.DB_INSTANCE_PATH}/..;mv {dn_name} {dn_name}_1; ls -l'
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        time.sleep(35)

        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_agent;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(cmd)
        file = self.node.sh(cmd).result()
        logger.info(file)
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_agent;tail -n 30 {file}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        flag = 'MissingDataInstDataDir'
        self.assertTrue(res.count(flag) >= 3, '执行失败' + text)
        cur_dn = macro.DB_INSTANCE_PATH.replace(f'{dn_name}', f'{dn_name}_1')
        cmd = f'cd {cur_dn}/..;mv {dn_name}_1 {dn_name}; ls -l'
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def tearDown(self):
        text = '--step4:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        res = self.comsh.exec_cm_ctl_set('agent', 'alarm_report_max_count=1')
        self.assertTrue(res, '执行失败:' + text)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            time.sleep(5)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
